<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <!-- <el-form-item label="会员ID" prop="memberId">
        <el-input
          v-model="queryParams.memberId"
          placeholder="请输入会员ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="提现来源" prop="withdrawalSource">
        <!-- <el-input
          v-model="queryParams.withdrawalSource"
          placeholder="请输入提现来源"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
        <el-select
          v-model="queryParams.withdrawalSource"
          clearable
          placeholder="请选择提现来源"
        >
          <el-option
            v-for="item in withdrawalOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="提现金额" prop="withdrawalPrice">
        <el-input
          v-model="queryParams.withdrawalPrice"
          placeholder="请输入提现金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="提现积分" prop="usePoint">
        <el-input
          v-model="queryParams.usePoint"
          placeholder="请输入提现积分数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="收款码图片地址" prop="imgUrl">
        <el-input
          v-model="queryParams.imgUrl"
          placeholder="请输入收款码图片地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="状态" prop="state">
        <el-select
          v-model="queryParams.state"
          clearable
          placeholder="请选择状态"
        >
          <el-option
            v-for="item in stateOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
        <!-- <el-input
          v-model="queryParams.state"
          placeholder="请输入状态"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
      </el-form-item>
      <!-- <el-form-item label="备注" prop="remarks">
        <el-input
          v-model="queryParams.remarks"
          placeholder="请输入备注"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['yh:withdrawalrecords:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['yh:withdrawalrecords:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['yh:withdrawalrecords:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['yh:withdrawalrecords:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <el-table
      v-loading="loading"
      :data="recordsList"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- <el-table-column label="id" align="center" prop="id" /> -->
      <!-- <el-table-column label="会员ID" align="center" prop="memberId" /> -->
      <el-table-column
        label="会员昵称"
        align="center"
        width="110"
        prop="nickName"
      />
      <!-- <el-table-column label="会员头像" align="center" prop="face" /> -->
      <el-table-column label="会员头像" align="center"  >
        <template slot-scope="scope">
          <img v-if="scope.row.face" :src="baseUrl + scope.row.face" alt="" style="width: 130px;height: 130px;" />
        </template>
      </el-table-column>

      <el-table-column label="手机号码" align="center" prop="mobile" />
      <el-table-column
        label="会员等级"
        align="center"
        width="110"
        prop="memberLevel"
      />
      <el-table-column
        label="提现来源"
        align="center"
        width="110"
        prop="withdrawalText"
      />
      <el-table-column
        label="提现金额"
        align="center"
        width="110"
        prop="withdrawalPrice"
      />
      
      <el-table-column label="二维码" align="center">
        <template slot-scope="scope"  >
          <img v-if="scope.row.imgUrl" :src="baseUrl + scope.row.imgUrl" class="avatar"  style="width: 130px;height: 130px;"/>
        </template>
      </el-table-column>
      <el-table-column
        label="提现积分"
        width="120"
        align="center"
        prop="usePoint"
      />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <!-- <el-table-column label="收款码图片地址" align="center" prop="imgUrl" /> -->
      <el-table-column
        label="状态"
        align="center"
        width="80"
        prop="statusText"
      />
      <!-- <el-table-column label="备注" align="center" prop="remarks" /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.state == '1'"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['yh:withdrawalrecords:edit']"
            >修改</el-button
          >
          <el-button size="mini" type="text" v-if="scope.row.state != '1'"
            >暂无</el-button
          >
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['yh:withdrawalrecords:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户提现记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="会员昵称" prop="nickName">
          <el-input
            v-model="form.nickName"
            placeholder="请输入会员ID"
            :disabled="true"
          />
        </el-form-item>
        <!-- <el-form-item label="提现来源" prop="withdrawalSource">
          <el-input v-model="form.withdrawalSource" placeholder="请输入提现来源" />
        </el-form-item> -->
        <el-form-item label="提现金额" prop="withdrawalPrice">
          <el-input
            v-model="form.withdrawalPrice"
            placeholder="请输入提现金额"
            :disabled="true"
          />
        </el-form-item>
        <el-form-item label="使用积分" prop="usePoint">
          <el-input
            v-model="form.usePoint"
            placeholder="请输入使用积分"
            :disabled="true"
          />
        </el-form-item>
        <el-form-item label="收款码图片">
          <img v-if="form.imgUrl" :src="baseUrl + form.imgUrl" alt="" style="width: 240px;height: 240px;"/>
          <!-- <el-input v-model="form.imgUrl" placeholder="请输入收款码图片地址" /> -->
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-select v-model="form.state" clearable placeholder="请选择状态">
            <el-option
              v-for="item in stateOptionsnew"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
          <!-- <el-input v-model="form.state" placeholder="请输入状态" /> -->
        </el-form-item>
        <!-- <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" placeholder="请输入备注" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listRecords,
  getRecords,
  delRecords,
  addRecords,
  updateRecords,
} from "@/api/yh/withdrawalRecord";
import baseUrl from "@/api/baseUrl";
export default {
  name: "Records",
  data() {
    return {
      baseUrl: baseUrl,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户提现记录表格数据
      recordsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        memberId: null,
        withdrawalSource: null,
        withdrawalPrice: null,
        usePoint: null,
        imgUrl: null,
        state: null,
        remarks: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        memberId: [
          { required: true, message: "会员ID不能为空", trigger: "blur" },
        ],
        withdrawalSource: [
          { required: true, message: "提现来源不能为空", trigger: "blur" },
        ],
      },
      withdrawalOptions: [
        {
          value: "POINT",
          label: "积分",
        },
        {
          value: "COMMISSION",
          label: "佣金",
        },
      ],
      stateOptions: [
        {
          value: 1,
          label: "申请中",
        },
        {
          value: 2,
          label: "申请通过",
        },
        {
          value: 3,
          label: "申请拒绝",
        },
      ],
      stateOptionsnew: [
        {
          value: 2,
          label: "申请通过",
        },
        {
          value: 3,
          label: "申请拒绝",
        },
      ],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户提现记录列表 */
    getList() {
      this.loading = true;
      listRecords(this.queryParams).then((response) => {
        let text = ["申请中", "申请通过", "申请拒绝"];
        this.recordsList = response.rows.map((item) => {
          return {
            ...item,
            statusText: text[item.state - 1],
            withdrawalText: item.withdrawalSource == "POINT" ? "积分" : "佣金",
          };
        });
        console.log();
        // this.recordsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        memberId: null,
        withdrawalSource: null,
        withdrawalPrice: null,
        usePoint: null,
        imgUrl: null,
        state: null,
        remarks: null,
        createTime: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户提现记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getRecords(id).then((response) => {
        this.form = response.data;
        this.form.state = ''
        this.open = true;
        console.log(this.form, "this.form");
        this.title = "修改用户提现记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateRecords(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRecords(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除用户提现记录编号为"' + ids + '"的数据项？')
        .then(function () {
          return delRecords(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "yh/withdrawal/record/export",
        {
          ...this.queryParams,
        },
        `records_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
